//
//  BallProgress.m
//  progress
//
//  Created by Stan on 2017-01-27.
//  Copyright © 2017 stan. All rights reserved.
//

#import "BallProgress.h"

@interface BallProgress ()
@property(assign,nonatomic)CGFloat startAngle;
@property(assign,nonatomic)CGFloat endAngle;

@property(weak,nonatomic) IBOutlet UILabel *progressLabel;


@end


@implementation BallProgress


- (void)drawRect:(CGRect)rect {
    CGPoint origin = CGPointMake(100, 100);
    CGFloat radius = 95.0f;
    
    UIBezierPath *ballPath = [UIBezierPath bezierPathWithArcCenter:origin radius:radius startAngle:_startAngle endAngle:_endAngle clockwise:YES];
    
    [[UIColor purpleColor]set];
    [ballPath fill];
    
//    在球形的外面绘制一个描边空心的圆形，不然很难看
    UIBezierPath *strokePath = [UIBezierPath bezierPathWithArcCenter:origin radius:radius startAngle:0 endAngle:-0.00000001 clockwise:YES];
    [[UIColor lightGrayColor]set];
    [strokePath stroke];
}


- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    
//    设置起始点，位置是根据进度动态变换的
    self.startAngle = M_PI_2 - progress * M_PI;
    self.endAngle = M_PI_2 + progress * M_PI;
    
    self.progressLabel.text = [NSString stringWithFormat:@"%0.2f%%",progress * 100];
    
    [self setNeedsDisplay];
}

@end
